package com.cyt.website.service.impl;

import cn.hutool.core.util.StrUtil;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.DictCode;
import com.cyt.base.util.ErrorCode;
import com.cyt.sys.model.po.SysFile;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.model.vo.SysFileVo;
import com.cyt.sys.service.SysFileService;
import com.cyt.sys.service.impl.BaseServiceImpl;
import com.cyt.website.dao.WebsiteApplicationDao;
import com.cyt.website.model.po.WebsiteApplication;
import com.cyt.website.model.vo.WebsiteApplicationVo;
import com.cyt.website.service.WebsiteApplicationCategoryService;
import com.cyt.website.service.WebsiteApplicationService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 官网应用案例管理-官网应用案例管理Service层实现类
 *
 * @author congcong tao
 * @date 2019-8-13 9:49:42
 */
@Service("websiteApplicationService")
public class WebsiteApplicationServiceImpl extends BaseServiceImpl<WebsiteApplication> implements WebsiteApplicationService {

    @Resource
    private WebsiteApplicationCategoryService websiteApplicationCategoryService;
    @Resource
    private SysFileService sysFileService;
    @Resource
    private WebsiteApplicationDao websiteApplicationDao;

    @Autowired
    public void setBaseDao(WebsiteApplicationDao websiteApplicationDao) {
        this.baseDao = websiteApplicationDao;
    }

    /**
     * 查询对象集合
     *
     * @param websiteApplicationVo 对象
     * @return List 对象集合
     * @author congcong tao
     * @date 2019-8-13 9:49:42
     */
    @Override
    public List<WebsiteApplication> listExample(WebsiteApplicationVo websiteApplicationVo) {
        Example example = new Example(WebsiteApplication.class);
        example.setOrderByClause(" create_time desc");
        Example.Criteria criteria = example.createCriteria();
        if (StrUtil.isNotEmpty(websiteApplicationVo.getName())) {
            criteria.andLike("name", "%" + websiteApplicationVo.getName() + "%");
        }
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        return websiteApplicationDao.selectByExample(example);
    }

    /**
     * 分页查询
     *
     * @param websiteApplicationVo 接受参数对象
     * @param pageNum          分页数
     * @param pageSize         每页显示条数
     * @return ReturnResult
     * @author congcong tao
     * @date 2019-8-13 9:49:42
     */
    @Override
    public ReturnResult page(WebsiteApplicationVo websiteApplicationVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        List<WebsiteApplication> list = listExample(websiteApplicationVo);
        PageInfo<WebsiteApplication> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 添加或更新
     *
     * @param websiteApplicationVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author congcong tao
     * @date 2019-8-13 9:49:42
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdate(WebsiteApplicationVo websiteApplicationVo, SysUser sysUser) {
        if (StrUtil.isEmpty(websiteApplicationVo.getId())) {
            WebsiteApplication websiteApplication = new WebsiteApplication(sysUser);
            websiteApplication.setName(websiteApplicationVo.getName());
            websiteApplication.setPhoto(websiteApplicationVo.getPhoto());
            websiteApplication.setPath(getFilePath(websiteApplicationVo.getPhoto()));
            websiteApplication.setUrl(websiteApplicationVo.getUrl());
            websiteApplication.setQrCode(websiteApplicationVo.getQrCode());
            websiteApplication.setDescription(websiteApplicationVo.getDescription());
            websiteApplication.setContent(websiteApplicationVo.getContent());
            websiteApplication.setStatus(websiteApplicationVo.getStatus());
            websiteApplication.setOrderNum(websiteApplicationVo.getOrderNum());
            websiteApplication.setSeoKeywords(websiteApplicationVo.getSeoKeywords());
            websiteApplication.setSeoDescription(websiteApplicationVo.getSeoDescription());
            int num = websiteApplicationDao.insert(websiteApplication);
            if (num > 0) {
                // 分类保存
                String categoryIds = websiteApplicationVo.getCategoryIds();
                websiteApplicationCategoryService.batchSave(websiteApplication.getId(), categoryIds, sysUser);
                return new ReturnResult(BaseConstant.DATABASE_SUCCEED_INSERT);
            } else {
                return new ReturnResult(ErrorCode.CODE_1, BaseConstant.DATABASE_ERROR_INSERT);
            }
        } else {
            WebsiteApplication websiteApplication = websiteApplicationDao.selectByPrimaryKey(websiteApplicationVo.getId());
            websiteApplication.setName(websiteApplicationVo.getName());
            websiteApplication.setPhoto(websiteApplicationVo.getPhoto());
            websiteApplication.setPath(getFilePath(websiteApplicationVo.getPhoto()));
            websiteApplication.setUrl(websiteApplicationVo.getUrl());
            websiteApplication.setQrCode(websiteApplicationVo.getQrCode());
            websiteApplication.setDescription(websiteApplicationVo.getDescription());
            websiteApplication.setContent(websiteApplicationVo.getContent());
            websiteApplication.setStatus(websiteApplicationVo.getStatus());
            websiteApplication.setOrderNum(websiteApplicationVo.getOrderNum());
            websiteApplication.setSeoKeywords(websiteApplicationVo.getSeoKeywords());
            websiteApplication.setSeoDescription(websiteApplicationVo.getSeoDescription());
            websiteApplication.setUpdateTime(new Date());
            websiteApplication.setUpdateUser(sysUser.getId());
            int num = websiteApplicationDao.updateByPrimaryKey(websiteApplication);
            if (num > 0) {
                // 分类保存
                String categoryIds = websiteApplicationVo.getCategoryIds();
                websiteApplicationCategoryService.batchSave(websiteApplication.getId(), categoryIds, sysUser);
                return new ReturnResult(BaseConstant.DATABASE_SUCCEED_UPDATE);
            } else {
                return new ReturnResult(ErrorCode.CODE_1, BaseConstant.DATABASE_ERROR_UPDATE);
            }
        }
    }

    /**
     * 根据条件逻辑删除
     *
     * @param websiteApplicationVo 参数接受对象
     * @param sysUser 当前用户
     * @return ReturnResult
     * @author congcong tao
     * @date 2019-8-13 9:49:42
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult remove(WebsiteApplicationVo websiteApplicationVo, SysUser sysUser) {
        //要修改的字段
        WebsiteApplication websiteApplication = new WebsiteApplication();
        websiteApplication.setRemoveTag(BaseConstant.REMOVE_TAG_YES);
        websiteApplication.setUpdateUser(sysUser.getId());
        websiteApplication.setUpdateTime(new Date());
        //筛选条件
        Example example = new Example(WebsiteApplication.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(websiteApplicationVo.getId())) {
            criteria.andIn("id", Arrays.asList(websiteApplicationVo.getId().split(",")));
        }
        criteria.andEqualTo("removeTag", BaseConstant.REMOVE_TAG_NO);
        websiteApplicationDao.updateByExampleSelective(websiteApplication, example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * 根据条件物理删除
     *
     * @param websiteApplicationVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author congcong tao
     * @date 2019-8-13 9:49:42
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult delete(WebsiteApplicationVo websiteApplicationVo, SysUser sysUser) {
        //筛选条件
        Example example = new Example(WebsiteApplication.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(websiteApplicationVo.getId())) {
            criteria.andIn("id", Arrays.asList(websiteApplicationVo.getId().split(",")));
        }
        websiteApplicationDao.deleteByExample(example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    @Override
    public List<WebsiteApplication> selectByCondition(WebsiteApplicationVo websiteApplicationVo) {
        return websiteApplicationDao.selectByCondition(websiteApplicationVo);
    }

    private String getFilePath(String ObjectId) {
        SysFileVo sysFileVo = new SysFileVo(ObjectId);
        List<SysFile> sysFileList = sysFileService.listExample(sysFileVo);
        if (sysFileList != null && sysFileList.size() == 1) {
            return sysFileList.get(0).getPath();
        }
        return "";
    }
}